package com.example.tests;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;

import com.example.fw.ApplicationManager;

public class TestBase {

	protected static ApplicationManager app;
	private int checkCounter;
	private int checkFrequency;

	@BeforeClass
	public void setUp() throws Exception {
		String configFile = System.getProperty("configFile",
				"firefox.properties");
		Properties properties = new Properties();
		properties.load(new FileReader(new File(configFile)));
		app = new ApplicationManager(properties);

		checkCounter = 0;
		checkFrequency = Integer.parseInt(properties.getProperty(
				"check.frequency", "0"));
	}

	@AfterTest
	public void tearDown() throws Exception {
		app.stop();

	}

	public static List<Object[]> wrapGroupDataForDataProvider(
			List<GroupData> groups) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (GroupData group : groups) {
			list.add(new Object[] { group });
		}
		return list;
	}

	public static List<Object[]> wrapContactsForDataProvider(
			List<ContactData> contacts) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (ContactData contact : contacts) {
			list.add(new Object[] { contact });
		}
		return list;
	}

	public String generationRandomString() {
		Random rnd = new Random();

		if (rnd.nextInt(3) == 0) {
			return "";
		} else {
			return " " + rnd.nextInt();
		}
	}

	protected boolean wantToCheck() {
		checkCounter++;
		if (checkCounter > checkFrequency) {
			checkCounter = 0;
			return true;
		} else {
			return false;
		}
	}
}
